package com.netsdk.lht2.handle.support;

import com.netsdk.demo.util.DateUtil;
import com.netsdk.demo.util.sql.EventDto;
import com.netsdk.lht2.handle.EventHandle;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.structure.DEV_EVENT_TRAFFIC_THROW_INFO;
import com.netsdk.lib.structure.NET_TIME_EX;
import com.sun.jna.Pointer;

import java.util.Date;

/*
* 行人检测处理
* */
public class EVENT_IVS_TRAFFIC_PEDESTRAIN_HANDLE extends EventHandle {

    @Override
    public void handle(int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) {
        EventDto dto = new EventDto();
        dto.setEvent_name("行人检测");
        dto.setEvent_code(dwAlarmType+"");
        //创建事件对象
        DEV_EVENT_TRAFFIC_THROW_INFO msg = new DEV_EVENT_TRAFFIC_THROW_INFO();
        //加载事件新信息
        ToolKits.GetPointerData(pAlarmInfo, msg);
        // 通道号 对应设备
        String nChannelID = getChannelMapping(msg.nChannelID);
        System.out.println("nChannelID:" + msg.nChannelID);
        dto.setChannel(nChannelID);
        //事件名称
        byte[] szName = msg.szName;
        //相对事件时间戳(单位是毫秒)
        double pts = msg.PTS;
        dto.setEvent_time(DateUtil.format(new Date((long) pts), "yyyy-MM-dd HH:mm:ss.SSS"));
        //UTC时间
        NET_TIME_EX UTC = msg.UTC;
        System.out.println("UTC:" + UTC.toStringTime());
        dto.setEvent_utc_time(UTC.toStringTime());
        //事件id
        int nEventID = msg.nEventID;

        dto.setEvent_id(nEventID+"");
        //事件对应文件信息
        NetSDKLib.NET_EVENT_FILE_INFO stuFileInfo = msg.stuFileInfo;
        dto.setEvent_content(stuFileInfo.toString());
        //图片分辨率
        NetSDKLib.NET_RESOLUTION_INFO stuResolution = msg.stuResolution;
        dto.setImg_fbl(stuResolution.toString());
        //图片
        int picSizes = 0;
        NetSDKLib.NET_PIC_INFO stPicInfo = msg.stuObject.stPicInfo;
        //图片保存
        if (pBuffer != null) {
            String pName = System.currentTimeMillis() + "related_EVENT_IVS_DREGS_UNCOVERED.jpg";
            String picture = picturePath + "/" + pName;
            if(null != pBuffer){
                dto.setImg_name(pName);
            }
            ToolKits.savePicture(pBuffer, 0, dwBufSize, picture);
        }
        //抓图标志(按位),0位:"*",1位:"Timing",2位:"Manual",3位:"Marked",4位:"Event",5位:"Mosaic",6位:"Cutout"
        int dwSnapFlagMask = msg.dwSnapFlagMask;
        dto.setImg_mask(dwSnapFlagMask+"");
        //事件动作,0表示脉冲事件,1表示持续性事件开始,2表示持续性事件结束
        byte bEventAction = msg.bEventAction;
        dto.setEvent_dz(String.valueOf(bEventAction));
        //智能事件所属大类
        int ClassType = msg.stuIntelliCommInfo.emClassType;
        dto.setEvent_class(ClassType+"");
        //对应车道号
        int nLane = msg.nLane;
        dto.setLane(nLane+"");
        // 数据入库
        insertEventDto(dto);
    }
}
